package ui;

import java.text.ParseException;
import java.util.Date;
import java.util.Scanner;
import java.text.SimpleDateFormat;
import entity.User;
import entity.Lab;
import entity.LabReservation;
import dao.LabDAO;
import dao.LabReservationDAO;

public class StudentUI {
    private final Scanner scanner = new Scanner(System.in);
    private final LabDAO labDAO = new LabDAO();
    private final LabReservationDAO reservationDAO = new LabReservationDAO();
    private final User currentUser;
    private final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");

    public StudentUI(User currentUser) {
        this.currentUser = currentUser;
    }

    public void showMenu() {
        while (true) {
            System.out.println("\n=== 学生菜单 ===");
            System.out.println("1. 查看实验室可用情况");
            System.out.println("2. 查看我的预约");
            System.out.println("3. 预约实验室");
            System.out.println("4. 退出");
            System.out.print("请选择操作：");

            int choice = scanner.nextInt();
            scanner.nextLine();

            switch (choice) {
                case 1:
                    checkLabAvailability();
                    break;
                case 2:
                    viewMyReservations();
                    break;
                case 3:
                    reserveLab();
                    break;
                case 4:
                    return;
                default:
                    System.out.println("无效选择");
            }
        }
    }

    // 查看实验室可用情况
    private void checkLabAvailability() {
        System.out.println("\n=== 实验室可用情况 ===");
        labDAO.listAllLabs().forEach(lab ->
                System.out.printf("ID: %d, 名称: %s, 状态: %s, 容量: %d\n",
                        lab.getLabId(), lab.getLabName(), lab.getStatus(), lab.getCapacity())
        );
    }

    // 查看我的预约
    private void viewMyReservations() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        System.out.println("\n=== 我的预约记录 ===");
        reservationDAO.getReservationsByUser(currentUser.getUserId()).forEach(res -> {
            Lab lab = labDAO.getLabById(res.getLabId());
            System.out.printf("实验室: %s（ID: %d）, 时间: %s - %s, 用途: %s\n",
                    lab.getLabName(), lab.getLabId(),
                    sdf.format(res.getStartTime()), sdf.format(res.getEndTime()),
                    res.getPurpose() == null ? "无" : res.getPurpose());
        });
    }

    private void reserveLab() {
        System.out.print("请输入实验室ID：");
        int labId = scanner.nextInt();
        scanner.nextLine();

        Lab lab = labDAO.getLabById(labId);
        if (lab == null) {
            System.out.println("实验室不存在");
            return;
        }

        System.out.print("请输入开始时间（格式yyyy-MM-dd HH:mm）：");
        String startStr = scanner.nextLine();
        System.out.print("请输入结束时间（格式yyyy-MM-dd HH:mm）：");
        String endStr = scanner.nextLine();
        System.out.print("请输入使用目的：");
        String purpose = scanner.nextLine();

        try {
            Date startTime = sdf.parse(startStr);
            Date endTime = sdf.parse(endStr);

            // 检查班级权限
            if (!reservationDAO.canStudentReserveLab(labId, currentUser.getUserId(), startTime, endTime)) {
                System.out.println("您的老师没有正在预约该实验室，无法预约");
                return;
            }
            LabReservation reservation = new LabReservation();
            reservation.setLabId(labId);
            reservation.setUserId(currentUser.getUserId());
            reservation.setStartTime(startTime);
            reservation.setEndTime(endTime);
            reservation.setPurpose(purpose);

            if (reservationDAO.addReservation(reservation)) {
                System.out.println("实验室预约成功");
            } else {
                System.out.println("预约失败");
            }
        } catch (ParseException e) {
            System.out.println("时间格式错误");
        }
    }
}
